#include "SCA830.h"
module SCA830P
{
	provides interface Read<uint16_t> as X;
	provides interface Read<uint16_t> as Temp;
	provides interface SplitControl as SensorControl;
	
	uses interface SCA830Logic;
}
implementation
{
	uint8_t state = STATE_OFF;
	error_t opResult;
	uint16_t opValue;
	
	task void SignalDone()
	{
		if(state == STATE_RX)
		{
			signal X.readDone(opResult, opValue);
			state = STATE_IDLE;
		}
		else if(state == STATE_RTEMP)
		{
			signal Temp.readDone(opResult, opValue);
			state = STATE_IDLE;
		}
		
		else if(state == STATE_STARTING)
		{
			signal SensorControl.startDone(opResult);
			state = STATE_IDLE;
		}
		
		else if(state == STATE_STOPPING)
		{
			signal SensorControl.startDone(opResult);
			state = STATE_OFF;
		}
		
		
	}
	
	command error_t X.read()
	{
		if(state == STATE_IDLE) 
		{
			state = STATE_RX;
			return call SCA830Logic.GetX();
		}
		else return FAIL;
	}
	command error_t Temp.read()
	{
		if(state == STATE_IDLE) 
		{
			state = STATE_RTEMP;
			return call SCA830Logic.GetTemp();
		}
		else return FAIL;
	}
	
	command error_t SensorControl.start()
	{
		state = STATE_STARTING;
		return call SCA830Logic.PowerOn();
	}

	command error_t SensorControl.stop()
	{
		state = STATE_STOPPING;
		return call SCA830Logic.PowerOn();
	}

	async event void SCA830Logic.PowerOnDone(error_t success)
	{
		opResult = success;
		post SignalDone();
	}

	async event void SCA830Logic.PowerOffDone(error_t success)
	{
		opResult = success;
		post SignalDone();
	}

	async event void SCA830Logic.ResetDone(error_t success){
		// TODO Auto-generated method stub
	}

	async event void SCA830Logic.GetStatusDone(error_t success, uint8_t status){
		// TODO Auto-generated method stub
	}

	async event void SCA830Logic.GetInterruptDone(error_t success, uint8_t interrupt){
		// TODO Auto-generated method stub
	}

	async event void SCA830Logic.GetControlDone(error_t success, uint8_t control){
		// TODO Auto-generated method stub
	}

	async event void SCA830Logic.SetControlDone(error_t success){
		// TODO Auto-generated method stub
	}

	async event void SCA830Logic.GetXDone(error_t success, uint16_t xData)
	{
		opResult = success;
		opValue = xData;
		
		post SignalDone();
	}

	async event void SCA830Logic.GetTempDone(error_t success, uint16_t tempData)
	{
		opResult = success;
		opValue = tempData;
		
		post SignalDone();
	}
}